﻿using System;
using System.Text;

namespace DataHandlerTest
{
    public class HexFormat
    {
        private HexFormat() { }

        static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
			'9', 'A', 'B', 'C', 'D', 'E', 'F' };

        public static string Bytes2Hex(byte[] bytes)
        {
            if (bytes == null) return string.Empty;
            StringBuilder buf = new StringBuilder(bytes.Length << 1);
            for (int i = 0; i < bytes.Length; i++)
            {
                buf.Append(digits[(bytes[i] >> 4) & 0x0F]);
                buf.Append(digits[(bytes[i]) & 0x0F]);
            }
            return buf.ToString();
        }

        public static string Byte2Hex(byte data)
        {
            char[] c = new char[2];
            c[0] = digits[(data >> 4) & 0x0F];
            c[1] = digits[(data) & 0x0F];
            return new string(c);
            //return (Convert.Tostring(zahl,16).ToUpper());
        }

        public static string Str2Hex(string s)
        {
            System.Text.Encoding encoding = System.Text.Encoding.Default;
            byte[] bytes = encoding.GetBytes(s);
            return HexFormat.Bytes2Hex(bytes);
        }

        public static byte[] Hex2Bytes(string hex)
        {
            if (string.IsNullOrEmpty(hex)) return null;
            return Hex2Bytes(hex, 0, hex.Length);
        }

        public static byte[] Hex2Bytes(string hex, int offset, int Length)
        {
            try
            {
                byte[] bytes = new byte[Length / 2];
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = (byte)((HexDigit2Int32(hex[offset++]) << 4) | HexDigit2Int32(hex[offset++]));
                }
                return bytes;
            }
            catch
            {
                throw new Exception("Invalid hex sequense at " + offset);
            }
        }

        public static byte[] Hex2Bytes(char[] hex, int offset, int Length)
        {
            try
            {
                byte[] bytes = new byte[Length / 2];
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = (byte)((HexDigit2Int32(hex[offset++]) << 4) | HexDigit2Int32(hex[offset++]));
                }
                return bytes;
            }
            catch
            {
                throw new Exception("Invalid hex sequense at " + offset);
            }
        }

        public static byte Hex2Byte(char[] hex, int offset)
        {
            try
            {
                return (byte)((HexDigit2Int32(hex[offset++]) << 4) | HexDigit2Int32(hex[offset++]));
            }
            catch
            {
                throw new Exception("Invalid hex sequense at position "
                        + offset + " in '" + new string(hex) + '\'');
            }
        }

        public static byte Hex2Byte(string hex)
        {
            return Convert.ToByte(hex, 16);
        }

        public static string Hex2Str(string hex)
        {
            byte[] bytes = HexFormat.Hex2Bytes(hex);
            System.Text.Encoding encoding = System.Text.Encoding.Default;
            return encoding.GetString(bytes);
        }

        public static int HexDigit2Int32(char digit)
        {
            if (digit >= '0' && digit <= '9')
                return digit - '0';
            digit |= (char)0x20;
            if (digit >= 'a' && digit <= 'f')
                return digit - 'W'; // -'W' == -'a' + 10
            throw new Exception();
        }

        public static String Bytes2Unique(byte[] daten, int offset)
        {
            if (daten.Length - offset < 16)
            {
                byte[] temp = new byte[16];
                Array.Copy(daten, offset, temp, 0, daten.Length - offset);
                daten = temp;
            }
            char[] chars = new char[36];
            chars[8] = chars[13] = chars[18] = chars[23] = '-';

            chars[0] = digits[(daten[offset + 3] >> 4) & 0x0F];
            chars[1] = digits[(daten[offset + 3]) & 0x0F];
            chars[2] = digits[(daten[offset + 2] >> 4) & 0x0F];
            chars[3] = digits[(daten[offset + 2]) & 0x0F];
            chars[4] = digits[(daten[offset + 1] >> 4) & 0x0F];
            chars[5] = digits[(daten[offset + 1]) & 0x0F];
            chars[6] = digits[(daten[offset + 0] >> 4) & 0x0F];
            chars[7] = digits[(daten[offset + 0]) & 0x0F];

            chars[9] = digits[(daten[offset + 5] >> 4) & 0x0F];
            chars[10] = digits[(daten[offset + 5]) & 0x0F];
            chars[11] = digits[(daten[offset + 4] >> 4) & 0x0F];
            chars[12] = digits[(daten[offset + 4]) & 0x0F];

            chars[14] = digits[(daten[offset + 7] >> 4) & 0x0F];
            chars[15] = digits[(daten[offset + 7]) & 0x0F];
            chars[16] = digits[(daten[offset + 6] >> 4) & 0x0F];
            chars[17] = digits[(daten[offset + 6]) & 0x0F];

            chars[19] = digits[(daten[offset + 8] >> 4) & 0x0F];
            chars[20] = digits[(daten[offset + 8]) & 0x0F];
            chars[21] = digits[(daten[offset + 9] >> 4) & 0x0F];
            chars[22] = digits[(daten[offset + 9]) & 0x0F];

            chars[24] = digits[(daten[offset + 10] >> 4) & 0x0F];
            chars[25] = digits[(daten[offset + 10]) & 0x0F];
            chars[26] = digits[(daten[offset + 11] >> 4) & 0x0F];
            chars[27] = digits[(daten[offset + 11]) & 0x0F];
            chars[28] = digits[(daten[offset + 12] >> 4) & 0x0F];
            chars[29] = digits[(daten[offset + 12]) & 0x0F];
            chars[30] = digits[(daten[offset + 13] >> 4) & 0x0F];
            chars[31] = digits[(daten[offset + 13]) & 0x0F];
            chars[32] = digits[(daten[offset + 14] >> 4) & 0x0F];
            chars[33] = digits[(daten[offset + 14]) & 0x0F];
            chars[34] = digits[(daten[offset + 15] >> 4) & 0x0F];
            chars[35] = digits[(daten[offset + 15]) & 0x0F];
            return new String(chars);
        }
    }
}
